﻿@{
    ViewBag.Title = "订单详细";
    Layout = "~/Views/Shared/_OrderForm.cshtml";
}
<script>
    var keyValue = request('keyValue');
    $(function () {
        InitialPage();
        GetOrderEntryGrid();
        InitControl();
    });
    //初始化页面
    function InitialPage() {
        $(".bills").height($(window).height() - 88);
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $(".bills").height($(window).height() - 88);
            }, 200);
            e.stopPropagation();
        });
        //客户名称
        $("#F_CustomerId").ComboBox({
            url: "../../CustomerManage/Customer/GetListJson",
            id: "F_CustomerId",
            text: "F_FullName",
            description: "==请选择==",
            height: "360px",
            width: "280px",
            allowSearch: true
        });
        //销售人员
        $("#F_SellerId").ComboBoxTree({
            url: "../../BaseManage/User/GetTreeJson",
            description: "==请选择==",
            height: "360px",
            width: "280px",
            allowSearch: true
        });
        //收款方式
        $("#F_PaymentMode").ComboBox({
            url: "../../SystemManage/DataItemDetail/GetDataItemListJson",
            param: { EnCode: "Client_PaymentMode" },
            id: "F_ItemValue",
            text: "F_ItemName",
            description: "==请选择==",
            height: "200px"
        });
    }
    //初始化控件
    function InitControl() {
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../CustomerManage/Order/GetFormJson",
                param: { keyValue: keyValue },
                success: function (data) {
                    //主表
                    var order = data.order;
                    $("#form1").SetWebControls(order);
                    $("#F_DiscountSum").val(toDecimal(order.F_DiscountSum));
                    $("#F_Accounts").val(toDecimal(order.F_Accounts));
                    $("#F_SaleCost").val(toDecimal(order.F_SaleCost));
                    //明细
                    var orderEntry = data.orderEntry;
                    $("#gridTable").find('[role=row]').each(function (i) {
                        var row = orderEntry[i - 1];
                        if (row != undefined) {
                            $(this).find('td[aria-describedby="gridTable_F_OrderEntryId"]').html(row.F_OrderEntryId);
                            $(this).find('td[aria-describedby="gridTable_F_ProductName"]').html(row.F_ProductName);
                            $(this).find('td[aria-describedby="gridTable_F_ProductCode"]').html(row.F_ProductCode);
                            $(this).find('td[aria-describedby="gridTable_F_UnitId"]').html(row.F_UnitId);
                            $(this).find('td[aria-describedby="gridTable_F_Qty"]').html(toDecimal(row.F_Qty));
                            $(this).find('td[aria-describedby="gridTable_F_Price"]').html(toDecimal(row.F_Price));
                            $(this).find('td[aria-describedby="gridTable_F_Amount"]').html(toDecimal(row.F_Amount));
                            $(this).find('td[aria-describedby="gridTable_F_TaxRate"]').html(row.F_TaxRate);
                            $(this).find('td[aria-describedby="gridTable_F_Taxprice"]').html(toDecimal(row.F_Taxprice));
                            $(this).find('td[aria-describedby="gridTable_F_Tax"]').html(toDecimal(row.F_Tax));
                            $(this).find('td[aria-describedby="gridTable_F_TaxAmount"]').html(toDecimal(row.F_TaxAmount));
                            $(this).find('td[aria-describedby="gridTable_F_Description"]').html(row.F_Description);
                        }
                    });
                    //合计
                    var TotalQty = 0.00, TotalPrice = 0.00, TotalAmount = 0.00, TotalTaxprice = 0.00, TotalTax = 0.00, TotalTaxAmount = 0.00;
                    $("#gridTable").find('[role=row]').each(function (i) {
                        var Qty = $(this).find('td:eq(5)').html();
                        if (Qty != "" && Qty != undefined && Qty != '&nbsp;') {
                            TotalQty += Number(Qty);
                        }
                        var Price = $(this).find('td:eq(6)').html();
                        if (Price != "" && Price != undefined && Price != '&nbsp;') {
                            TotalPrice += Number(Price);
                        }
                        var Amount = $(this).find('td:eq(7)').html();
                        if (Amount != "" && Amount != undefined && Amount != '&nbsp;') {
                            TotalAmount += Number(Amount);
                        }
                        var Taxprice = $(this).find('td:eq(9)').html();
                        if (Taxprice != "" && Taxprice != undefined && Taxprice != '&nbsp;') {
                            TotalTaxprice += Number(Taxprice);
                        }
                        var Tax = $(this).find('td:eq(10)').html();
                        if (Tax != "" && Tax != undefined) {
                            TotalTax += Number(Tax);
                        }
                        var TaxAmount = $(this).find('td:eq(11)').html();
                        if (TaxAmount != "" && TaxAmount != undefined && TaxAmount != '&nbsp;') {
                            TotalTaxAmount += Number(TaxAmount);
                        }
                    });
                    $("#TotalQty").text(toDecimal(TotalQty));
                    $("#TotalPrice").text(toDecimal(TotalPrice));
                    $("#TotalAmount").text(toDecimal(TotalAmount));
                    $("#TotalTaxprice").text(toDecimal(TotalTaxprice));
                    $("#TotalTax").text(toDecimal(TotalTax));
                    $("#TotalTaxAmount").text(toDecimal(TotalTaxAmount));
                }
            });
        }
    }
    //加载明细
    function GetOrderEntryGrid() {
        var $grid = $("#gridTable");
        $grid.jqGrid({
            unwritten: false,
            datatype: "local",            height: '100%',
            autowidth: true,
            colModel: [
                { label: '主键', name: 'F_OrderEntryId', hidden: true },
                { label: '商品名称', name: "F_ProductName", width: 260, align: 'left', sortable: false, resizable: false },
                { label: '商品编号', name: "F_ProductCode", width: 100, align: 'center', sortable: false, resizable: false },
                { label: '单位', name: "F_UnitId", width: 100, align: 'center', sortable: false, resizable: false },
                { label: '数量', name: 'F_Qty', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '单价', name: 'F_Price', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '金额', name: 'F_Amount', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '税率(%)', name: 'F_TaxRate', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '含税单价', name: 'F_Taxprice', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '税额', name: 'F_Tax', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '含税金额', name: 'F_TaxAmount', width: 80, align: 'center', sortable: false, resizable: false },
                { label: '说明信息', name: 'F_Description', width: 200, align: 'left', sortable: false, resizable: false }            ],
            pager: false,
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            footerrow: true,
            gridComplete: function () {
                //合计
                $(this).footerData("set", {
                    "F_UnitId": "合计：",
                    "F_Qty": "<span id='TotalQty'>0.00</span>",
                    "F_Price": "<span id='TotalPrice'>0.00</span>",
                    "F_Amount": "<span id='TotalAmount'>0.00</span>",
                    "F_Taxprice": "<span id='TotalTaxprice'>0.00</span>",
                    "F_Tax": "<span id='TotalTax'>0.00</span>",
                    "F_TaxAmount": "<span id='TotalTaxAmount'>0.00</span>"
                });
                $('table.ui-jqgrid-ftable td[aria-describedby="gridTable_F_UnitId"]').prevUntil().css("border-right-color", "#fff");
            }
        });
        //表头合并
        $grid.jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders: [
              { startColumnName: 'F_ProductName', numberOfColumns: 3, titleText: '商品信息' },
              { startColumnName: 'F_Qty', numberOfColumns: 7, titleText: '价格信息' }
            ]
        });
        //默认添加13行 空行
        for (var i = 1; i < 13; i++) {
            var rowdata = {
                F_OrderEntryId: '',
                F_ProductName: '',
                F_ProductCode: '',
                F_UnitId: '',
                F_Qty: '',
                F_Price: '',
                F_Amount: '',
                F_TaxRate: '',
                F_Taxprice: '',
                F_Tax: '',
                F_TaxAmount: '',
                F_Description: '',
            }
            $grid.jqGrid('addRowData', i, rowdata);
        };
    }
    //打印
    function btn_print() {
        $(".bills").printTable();
    }
    //导出
    function btn_export() {
        location.href = "/CustomerManage/Order/ExportOrderEntry?orderId=" + keyValue;
    }
    //前单
    function btn_prev() {
        $.ajax({
            url: "../../CustomerManage/Order/GetPrevJson?keyValue=" + keyValue,
            type: "get",
            dataType: "json",
            async: false,
            success: function (data) {
                if (data != null) {
                    var src = top.contentPath + "/CustomerManage/Order/Detail?keyValue=" + data.F_OrderId;
                    window.location = src;
                    $.currentIframe().att('src', 'src');
                }
            }
        });
    }
    //后单
    function btn_next() {
        $.ajax({
            url: "../../CustomerManage/Order/GetNextJson?keyValue=" + keyValue,
            type: "get",
            dataType: "json",
            async: false,
            success: function (data) {
                if (data != null) {
                    var src = top.contentPath + "/CustomerManage/Order/Detail?keyValue=" + data.F_OrderId;
                    window.location = src;
                    $.currentIframe().att('src', 'src');
                }
            }
        });
    }
</script>
<div class="bills">
    <div class="printArea">
        <table class="form" style="width: 100%; margin-bottom: 10px;">
            <tr>
                <th class="formTitle" style="width: 60px;">客户名称</th>
                <td class="formValue">
                    <div id="F_CustomerId" type="select" class="ui-select"></div>
                </td>
                <th class="formTitle">销售人员</th>
                <td class="formValue">
                    <div id="F_SellerId" type="selectTree" class="ui-select"></div>
                </td>
                <th class="formTitle">单据日期</th>
                <td class="formValue">
                    <input id="F_OrderDate" type="text" class="form-control" />
                </td>
                <th class="formTitle">单据编号</th>
                <td class="formValue">
                    <input id="F_OrderCode" type="text" class="form-control" />
                </td>
            </tr>
        </table>
    </div>
    <div class="gridPanel printArea">
        <table id="gridTable"></table>
    </div>
    <div class="printArea">
        <textarea id="F_Description" placeholder="暂无备注信息" class="form-control" style="width: 100%; height: 50px; margin-top: 10px;"></textarea>
        <table class="form" style="width: 100%; margin-top: 5px;">
            <tr>
                <th class="formTitle" style="width: 60px;">优惠金额</th>
                <td class="formValue">
                    <input id="F_DiscountSum" type="text" class="form-control" value="0.00" />
                </td>
                <th class="formTitle">收款金额</th>
                <td class="formValue">
                    <input id="F_Accounts" type="text" class="form-control" value="0.00" />
                </td>
                <th class="formTitle">收款日期</th>
                <td class="formValue">
                    <input id="F_PaymentDate" type="text" class="form-control" />
                </td>
                <th class="formTitle">收款方式</th>
                <td class="formValue">
                    <div id="F_PaymentMode" type="select" class="ui-select"></div>
                </td>
            </tr>
            <tr>
                <th class="formTitle" style="width: 60px;">销售费用</th>
                <td class="formValue">
                    <input id="F_SaleCost" type="text" class="form-control" value="0.00" />
                </td>
                <th class="formTitle">制单人员</th>
                <td class="formValue">
                    <input id="F_CreateUserName" type="text" class="form-control" /></td>
                <th class="formTitle">合同编号</th>
                <td class="formValue">
                    <input id="F_ContractCode" type="text" class="form-control" />
                </td>
                <th class="formTitle">合同附件</th>
                <td class="formValue">
                    <input id="F_ContractFile" type="text" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">摘要信息</th>
                <td class="formValue" colspan="7">
                    <input id="F_AbstractInfo" type="text" class="form-control" />
                </td>
            </tr>
        </table>
    </div>
</div>
<div id="bottomField">
    <div class="btn-group">
        <a id="lr-print" class="btn btn-default" onclick="btn_print()"><i class="fa fa-print"></i>&nbsp;打印</a>
        <a id="lr-export" class="btn btn-default" onclick="btn_export()"><i class="fa fa-sign-out"></i>&nbsp;导出</a>
    </div>
    <div class="btn-group">
        <a id="lr-prev" class="btn btn-default" onclick="btn_prev()"><i class="fa fa-backward"></i>&nbsp;前单</a>
        <a id="lr-next" class="btn btn-default" onclick="btn_next()"><i class="fa fa-forward"></i>&nbsp;后单</a>
    </div>
</div>
